package com.stock;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.net.URI;

public class SentimentDriver {

    public static void main(String[] args) throws Exception {

        if (args.length < 3) {
            System.err.println("Usage: SentimentDriver <input> <output> <stopword-file>");
            System.exit(2);
        }

        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "Sentiment Word Count with Top 100");

        job.setJarByClass(SentimentDriver.class);
        job.setMapperClass(SentimentWordCountMapper.class);
        job.setReducerClass(SentimentWordCountReducer.class);

        // 输出 key-value 类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        // 输入输出路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 使用 MultipleOutputs 注册不同输出文件名
        MultipleOutputs.addNamedOutput(job, "positive", TextOutputFormat.class, Text.class, IntWritable.class);
        MultipleOutputs.addNamedOutput(job, "negative", TextOutputFormat.class, Text.class, IntWritable.class);

        // Reducer 设为 1 个，方便汇总所有词频
        job.setNumReduceTasks(1);

        // 添加停用词缓存文件
        job.addCacheFile(new URI(args[2]));

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
